﻿package {
	import flash.display.*;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.geom.*;
	
	public class CardControl extends Sprite {
		var movieClip:MovieClip;
		var img:Bitmap;
		/// 常量定义
		var AllCardData:Array = [
			0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,	//方块 A - K							  
			0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,	//红桃 A - K
			0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,	//黑桃 A - K
			0x4E,0x4F ];
		var MASK_COLOR:Number = 0xF0; //花色掩码
		var MASK_VALUE:Number = 0x0F; //数值掩码
		var CardWidth:uint = 80;
		var CardHeight:uint = 105;
		var CardSpace:uint = 20;
		var UserMode:Boolean = true;
		/// 逻辑变量
		var selectedArray:Array = []; // 选择扑克在cardDataArray中的index
		var cardDataArray:Array = []; // 扑克数据

		public function CardControl(mc:MovieClip) {
			movieClip = mc;			
			loadBitmap();
		}
		
		public function outCard():Array {
			if(selectedArray.length <=0 ) return [];			
			selectedArray.sort().reverse();
			
			var ret:Array = [];
			
			for each (var s in selectedArray) {
				this.removeChildAt(s);
				ret.push(cardDataArray[s]);
				cardDataArray.splice(s,1);
			}
			// 出牌后重置数据，重新设置控件
			selectedArray = [];
			this.setCardData(cardDataArray);
			return ret;
		}
		
		public function setCardData(cd:Array) {
			// 清除之前的child
			while(numChildren){removeChildAt(0)} 
			trace("setCardData:" + cd);
			cardDataArray = cd;
			var i:uint = 0;
			for each (var c in cd) {
				var color = c & MASK_COLOR;
				var value = c & MASK_VALUE;
				if(value > 13) value -= 13;
				color >>= 4;
				trace("color:" + color);
				trace("value:" + value);
				var cardBitmap:Bitmap = new Bitmap(new BitmapData(CardWidth,CardHeight));
					cardBitmap.bitmapData.copyPixels(img.bitmapData,
											   new Rectangle((value-1)*CardWidth,color*CardHeight,CardWidth,CardHeight),
											   new Point(0,0));
				var card:Sprite = new Sprite();
				card.addChild(cardBitmap);
				if(UserMode)
					card.addEventListener(MouseEvent.CLICK, cardClicked);
				addChild(card);
				card.x = i*CardSpace;
				card.y = 100;
				i++;
			}
			//var x:Number = 0x01;
		}
		
		private function loadBitmap() {
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
			var request:URLRequest = new URLRequest("CARD.png");
			loader.load(request);
		}
		
		private function loadingDone(event:Event) {
			img = Bitmap(event.target.loader.content);
			
			var rows:uint = 5;
			var cols:uint = 13;
			/*
			for(var i:uint=0; i<rows; i++) {
				for(var j:uint=0; j<cols; j++) {
					var cardBitmap:Bitmap = new Bitmap(new BitmapData(cardWidth,cardHeight));
					cardBitmap.bitmapData.copyPixels(img.bitmapData,
											   new Rectangle(j*cardWidth,i*cardHeight,cardWidth,cardHeight),
											   new Point(0,0));
					var card:Sprite = new Sprite();
					card.addChild(cardBitmap);
					card.addEventListener(MouseEvent.CLICK, cardClicked);
					addChild(card);
					card.x = j*(cardWidth+5) + 20;
					card.y = i*(cardHeight+5) + 20;
				}
			}
			*/
		}
		
		private function cardClicked(event:MouseEvent) {
			var card:Sprite = Sprite(event.target);	
			
			
			var cardIndex:int = this.getChildIndex(card);
			if(selectedArray.indexOf(cardIndex) >= 0) {
				selectedArray.splice(selectedArray.indexOf(cardIndex),1);
				card.y += 20;
			}
			else {
				selectedArray.push(cardIndex);
				card.y -= 20;
			}
			
			/*
			if(card.alpha < 1)
				card.alpha = 1;
			else
				card.alpha = .5;
			*/
		}
	}
}